
package com.financing.wap.util;


import java.util.Map;
import java.util.Set;

import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;



public class MemcacheCacheHelper {

	public static MemCachedClient mcc = new MemCachedClient();
	static{
		// 设置缓存服务器列表，当使用分布式缓存的时，可以指定多个缓存服务器。（这里应该设置为多个不同的服务器）
		String[] servers =
			{
					"api.memcache.superfa.cn:12000",
					"api.memcache.superfa.cn:12000"
//					"192.168.1.88:12000"
			};

		// 设置服务器权重
		Integer[] weights = {3, 2};
		// 创建一个Socked连接池实例
		SockIOPool pool = SockIOPool.getInstance();
		// 向连接池设置服务器和权重
		pool.setServers(servers);
		pool.setWeights(weights);
		pool.setNagle(false);
		pool.setSocketTO(3000);
		pool.setSocketConnectTO(0);
		// initialize the connection pool
		pool.initialize();
		MemcacheCacheHelper.mcc.set("otpFrequency",10);
		MemcacheCacheHelper.mcc.set("otpTimeOut", 60*60*1000L);
	}

	public static void main(String[] args) {
		//mcc.set("foo", "This is a test String");
		Map<String,Map<String,String>> map = mcc.statsItems();
		Set<String> s = map.keySet();

		System.out.println(">>> " + mcc.get("userInfo_1"));
		System.out.println(">>> " + s);
		///////////////////
		
////		Map<String,KeysBean> keylist=new HashMap<String,KeysBean>(); 
//        //遍历statsItems 获取items:2:number=14 
//        Map<String,Map<String,String>> statsItems=mcc.statsItems(); 
//        Map<String,String> statsItems_sub=null; 
//        String statsItems_sub_key=null; 
//        int items_number=0; 
//        String server=null; 
//        //根据items:2:number=14，调用statsCacheDump，获取每个item中的key 
//        Map<String,Map<String,String>> statsCacheDump=null; 
//        Map<String,String> statsCacheDump_sub=null; 
//        String statsCacheDumpsub_key=null; 
//        String statsCacheDumpsub_key_value=null; 
//           
//        for (Iterator iterator=statsItems.keySet().iterator();iterator.hasNext();) { 
//            server=(String) iterator.next(); 
//            statsItems_sub=statsItems.get(server); 
//            //System.out.println(server+"==="+statsItems_sub); 
//            for (Iterator iterator_item=statsItems_sub.keySet().iterator();iterator_item.hasNext();) { 
//                statsItems_sub_key=(String) iterator_item.next();                
//                //System.out.println(statsItems_sub_key+":=:"+bb); 
//                //items:2:number=14 
//                if (statsItems_sub_key.toUpperCase().startsWith("items:".toUpperCase()) && statsItems_sub_key.toUpperCase().endsWith(":number".toUpperCase())){ 
//                    items_number=Integer.parseInt(statsItems_sub.get(statsItems_sub_key).trim()); 
//                    //System.out.println(statsItems_sub_key+":=:"+items_number); 
//                    statsCacheDump=mcc.statsCacheDump(new String[]{server},Integer.parseInt(statsItems_sub_key.split(":")[1].trim()), items_number); 
//                       
//                    for (Iterator statsCacheDump_iterator=statsCacheDump.keySet().iterator();statsCacheDump_iterator.hasNext();) { 
//                        statsCacheDump_sub=statsCacheDump.get(statsCacheDump_iterator.next()); 
//                        //System.out.println(statsCacheDump_sub); 
//                        for (Iterator iterator_keys=statsCacheDump_sub.keySet().iterator();iterator_keys.hasNext();) { 
//                            statsCacheDumpsub_key=(String) iterator_keys.next(); 
//                            statsCacheDumpsub_key_value=statsCacheDump_sub.get(statsCacheDumpsub_key);                           
//                            //System.out.println(statsCacheDumpsub_key);//key是中文被编码了,是客户端在set之前编码的，服务端中文key存的是密文 
//                            //System.out.println(statsCacheDumpsub_key_value); 
//                               System.out.println("server==>"+Long.parseLong(statsCacheDumpsub_key_value.substring(1, statsCacheDumpsub_key_value.indexOf("b;")-1).trim())+"--"+Long.parseLong(statsCacheDumpsub_key_value.substring(statsCacheDumpsub_key_value.indexOf("b;")+2,statsCacheDumpsub_key_value.indexOf("s]")-1).trim()));
////                            keylist.put(URLDecoder.decode(statsCacheDumpsub_key, "UTF-8"), new KeysBean(server,Long.parseLong(statsCacheDumpsub_key_value.substring(1, statsCacheDumpsub_key_value.indexOf("b;")-1).trim()),Long.parseLong(statsCacheDumpsub_key_value.substring(statsCacheDumpsub_key_value.indexOf("b;")+2,statsCacheDumpsub_key_value.indexOf("s]")-1).trim()))); 
//                        } 
//                    } 
//                } 
//   
//            } 
//        } 
//       
		
		
		
	}
}

